package com.qdxwx.web.controller;

import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.common.utils.TKString;

import jakarta.servlet.http.HttpServletRequest;

/**
 * Controller基础继承类
 */
public class BaseController {

	private static final String VIEW_ROOT_PATH = "qdxwx/";
	private static final String VIEW_H5_PATH = "h5/";
	public static final String VIEW_ERROR = "/error/error";
	public static final String VIEW_ERROR_403 = "/error/403";
	public static final String VIEW_ERROR_404 = "/error/404";

	/**
	 * 获取页面路径
	 */
	public static String getViewPath(String path) {
		return VIEW_ROOT_PATH + path;
	}

	public static String getViewPathH5(String path) {
		return VIEW_H5_PATH + path;
	}

	/**
	 * 异常跳转
	 *
	 * @param request HttpServletRequest
	 * @param e       Exception
	 */
	public String toErrorView(HttpServletRequest request, Exception e) {
		return toErrorView(request, e.getMessage());
	}

	public String toErrorView(HttpServletRequest request, String message) {
		if (message != null) {
			request.setAttribute("message", TKString.queryKey(message));
		}
		return VIEW_ERROR;
	}

	public String toError404(HttpServletRequest request) {
		if (TKRequest.isMobileBrowse(request)) {
			//这里可以用H5的
			return VIEW_ERROR_404;
		}
		return VIEW_ERROR_404;
	}

	public String toLoginPage() {
		return "redirect:/auth/login.html";
	}
}
